<script setup lang="ts">
import { ref, shallowRef } from "vue";
import { PureTable } from "@pureadmin/table";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useUserClassifyTable } from "../componentsUtils/useUserClassifyTable";
import type { UserClassifyTableProps } from "../componentsUtils/type/userClassifyTypes";
import Delete from "~icons/ep/delete";
import EditPen from "~icons/ep/edit-pen";
import Refresh from "~icons/ep/refresh";
import AddFill from "~icons/ri/add-circle-line";
import { ElMessageBox } from "element-plus";

const props = withDefaults(defineProps<UserClassifyTableProps>(), {
  lesseeId: 0,
  tenantName: "",
  dealerId: 0
});

const tableRef = ref();
const formRef = shallowRef();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  buttonClass,
  deviceDetection,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  totalClassifyCount,
  activeClassifyCount,
  classifyUserCount
} = useUserClassifyTable(tableRef, props);

// 删除确认
function handleDeleteConfirm(row: any) {
  ElMessageBox.confirm(
    `确认要删除分类名称为【${row.Name}】的这条数据吗?`,
    "系统提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }
  )
    .then(() => {
      handleDelete(row);
    })
    .catch(() => {
      // 用户取消删除
    });
}
</script>

<template>
  <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
    <div :class="[deviceDetection() ? ['w-full', 'mt-2'] : 'w-[calc(100%)]']">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
      >
        <el-form-item label="搜索关键词：" prop="SearchTerm">
          <el-input
            v-model="form.SearchTerm"
            placeholder="请输入分类名称"
            clearable
            class="w-[180px]!"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <PureTableBar title="" :columns="columns" @refresh="onSearch">
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog('创建')"
          >
            新增分类
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            ref="tableRef"
            adaptive
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            style="height: calc(100vh - 500px)"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-dropdown>
                <el-button
                  class="ml-3 mt-[2px]"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon('ri:more-fill')"
                />
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>
                      <el-button
                        :class="buttonClass"
                        link
                        type="primary"
                        :size="size"
                        :icon="useRenderIcon(EditPen)"
                        @click.stop="openDialog('修改', row)"
                      >
                        修改
                      </el-button>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button
                        :class="buttonClass"
                        link
                        type="danger"
                        :size="size"
                        :icon="useRenderIcon(Delete)"
                        @click.stop="handleDeleteConfirm(row)"
                      >
                        删除
                      </el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

/* 调整操作列按钮样式 */
:deep(.el-table__cell) {
  .flex.space-x-2 {
    display: flex;
    gap: 8px;
  }
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
// 设置分页组件左对齐
:deep(.el-pagination) {
  justify-content: flex-start !important;
}

// 设置按钮容器左对齐
:deep(.flex.justify-between.w-full.h-\[60px\].p-4) {
  justify-content: flex-start !important;

  .flex.items-center.justify-around {
    justify-content: flex-start !important;
    margin-left: 0 !important;
  }
}
</style>
